package com.sugar.subscribe.course.module.business.controller.admin.serviceclass;

import com.sugar.subscribe.course.module.business.controller.app.serviceclass.vo.AppServiceClassRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.serviceclass.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.serviceclass.ServiceClassDO;
import com.sugar.subscribe.course.module.business.service.serviceclass.ServiceClassService;

@Tag(name = "管理后台 - 服务分类")
@RestController
@RequestMapping("/business/service-class")
@Validated
public class ServiceClassController {

    @Resource
    private ServiceClassService serviceClassService;

    @PostMapping("/create")
    @Operation(summary = "创建服务分类")
    @PreAuthorize("@ss.hasPermission('business:service-class:create')")
    public CommonResult<Long> createServiceClass(@Valid @RequestBody ServiceClassSaveReqVO createReqVO) {
        return success(serviceClassService.createServiceClass(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务分类")
    @PreAuthorize("@ss.hasPermission('business:service-class:update')")
    public CommonResult<Boolean> updateServiceClass(@Valid @RequestBody ServiceClassSaveReqVO updateReqVO) {
        serviceClassService.updateServiceClass(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/status")
    @Operation(summary = "更新服务分类状态")
    @PreAuthorize("@ss.hasPermission('business:service-class:update')")
    public CommonResult<Boolean> updateServiceClassStatus(@Valid @RequestBody ServiceClassUpdateStatusReqVO updateReqVO) {
        serviceClassService.updateServiceClassStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:service-class:delete')")
    public CommonResult<Boolean> deleteServiceClass(@RequestParam("id") Long id) {
        serviceClassService.deleteServiceClass(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除服务分类")
                @PreAuthorize("@ss.hasPermission('business:service-class:delete')")
    public CommonResult<Boolean> deleteServiceClassList(@RequestParam("ids") List<Long> ids) {
        serviceClassService.deleteServiceClassListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:service-class:query')")
    public CommonResult<ServiceClassRespVO> getServiceClass(@RequestParam("id") Long id) {
        ServiceClassDO serviceClass = serviceClassService.getServiceClass(id);
        return success(BeanUtils.toBean(serviceClass, ServiceClassRespVO.class));
    }


    @GetMapping("/all")
    @Operation(summary = "获得所有服务分类")
    @PreAuthorize("@ss.hasPermission('business:service-class:query')")
    public CommonResult<List<AppServiceClassRespVO>> getServiceClassAll() {
        return success(serviceClassService.getServiceClassAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务分类分页")
    @PreAuthorize("@ss.hasPermission('business:service-class:query')")
    public CommonResult<PageResult<ServiceClassRespVO>> getServiceClassPage(@Valid ServiceClassPageReqVO pageReqVO) {
        PageResult<ServiceClassDO> pageResult = serviceClassService.getServiceClassPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceClassRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:service-class:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceClassExcel(@Valid ServiceClassPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceClassDO> list = serviceClassService.getServiceClassPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务分类.xls", "数据", ServiceClassRespVO.class,
                        BeanUtils.toBean(list, ServiceClassRespVO.class));
    }

}